package com.jtjrenren.android.taxi.passenger.manger.push;


import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderAmountResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDispatchResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDriverIncomeResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OtherLoginResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.ReceiveConCmd;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.ReceiveControlCmd;
import com.jtjrenren.android.taxi.passenger.bean.protocl.BaseResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.CommonResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderAcceptResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderDriverCancleResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderFilterResponse;
import com.jtjrenren.android.taxi.passenger.bean.protocl.OrderPassgerIncarResponse;
import com.jtjrenren.android.taxi.passenger.manger.push.cmd.Protocl;

import org.greenrobot.eventbus.EventBus;

import com.library.utils.LogUtil;

/**
 * Created by Administrator on 2016/1/21.
 * 处理接收到的调度消息的任务线程
 */
public class PushEngine {
    public static void handMsg(String receiveCmd) {
        LogUtil.log("push", "处理回复消息文本格式为:" + receiveCmd);
        if (!AppManger.getInstance().isLogin()) {
            LogUtil.log("push", "处理回复消息文本格式为:用户没有登陆，进行拦截");
            return;
        }
        ReceiveConCmd receiveConCmd = ReceiveControlCmd.parseReceiveCmd(receiveCmd);
        if (receiveConCmd == null)
            return;
        if (!receiveConCmd.getCheckCode().equalsIgnoreCase(receiveConCmd.getCheckCodeCalc()))
            return;

        String msg_id = receiveConCmd.getMsg_id();
        String content = receiveConCmd.getContent();
        String center_msgserno = receiveConCmd.getSerialNo();
        BaseResponse baseResponse = null;

        LogUtil.log("push", "处理回复消息:" + receiveConCmd.toString());
        if (Protocl.MSG_ID_CENTER_RESPONSE.equals(msg_id)) {

            CommonResponse commonResponse = ReceiveControlCmd.parseCommonResponse(content);
            baseResponse = commonResponse;

            LogUtil.log("push", "处理通用回复:" + commonResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_DISPATCH.equals(msg_id)) {

            OrderDispatchResponse dispatchResponse = ReceiveControlCmd.parseOrderDispatchResponse(content);
            baseResponse = dispatchResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, dispatchResponse.getOrderSerilNo());

            LogUtil.log("push", "处理订单分发情况:" + dispatchResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_FILTER.equals(msg_id)) {

            OrderFilterResponse filterResponse = ReceiveControlCmd.parseOrderFilterResponse(content);
            baseResponse = filterResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, filterResponse.getOrderSeriNo());

            LogUtil.log("push", "处理订单正在筛选:" + filterResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_ACCEPT.equals(msg_id)) {

            OrderAcceptResponse acceptResponse = ReceiveControlCmd.parseOrderAccepctResponse(content);
            baseResponse = acceptResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, acceptResponse.getOrderSerialNo());

            LogUtil.log("push", "处理订单接收情况:" + acceptResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_DRIVER_INCOME.equals(msg_id)) {

            OrderDriverIncomeResponse driverIncomeResponse = ReceiveControlCmd.parseOrderDriverIncomResponse(content);
            baseResponse = driverIncomeResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, driverIncomeResponse.getOrderSerialNo());

            LogUtil.log("push", "处理司机正在接驾:" + driverIncomeResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_PASSGER_INCAR.equals(msg_id)) {

            OrderPassgerIncarResponse passgerIncarResponse = ReceiveControlCmd.parseOrderPassgerIncarResponse(content);
            baseResponse = passgerIncarResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, passgerIncarResponse.getOrderSerialNo());

            LogUtil.log("push", "处理乘客上车确认:" + passgerIncarResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_DRIVER_CANCLE.equals(msg_id)) {

            OrderDriverCancleResponse driverCancleResponse = ReceiveControlCmd.parseOrderDriverCancleResponse(content);
            baseResponse = driverCancleResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, driverCancleResponse.getOrderSerialNo());

            LogUtil.log("push", "处理司机取消订单:" + driverCancleResponse.toString());

        } else if (Protocl.MSG_ID_CENTER_ORDER_AMOUNT.equals(msg_id)) {

            OrderAmountResponse amountResponse = ReceiveControlCmd.parseOrderAmountResponse(content);
            baseResponse = amountResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, amountResponse.getOrderSerialNo());

            LogUtil.log("push", "处理订单车费信息:" + amountResponse.toString());
        } else if (Protocl.MSG_ID_CENTER_RESPONSE_OTHERLOGIN.equals(msg_id)) {
            OtherLoginResponse amountResponse = ReceiveControlCmd.parseOtherDutyOn(content);
            baseResponse = amountResponse;

            sendResponseToCenter(center_msgserno, msg_id, Protocl.RESULT_SUCCESS, "0000000000");

            LogUtil.log("push", "处理其他端登陆问题:");
        }
        if (baseResponse != null)
            EventBus.getDefault().post(baseResponse);
    }


    /**
     * 向平台回复通用应答
     *
     * @param center_msgserno
     * @param centermsgId
     * @param result
     * @param orderSerno
     */
    private static void sendResponseToCenter(String center_msgserno, String centermsgId, int result, String orderSerno) {
        String commnResponseCmd = ControlCmd.getCommnResponseCmd(center_msgserno, centermsgId, result, orderSerno);
        AppManger.getInstance().sendMsgToControl(commnResponseCmd);
    }
}
